﻿using STF.Blog.Dtos;
using STF.Blog.IRepository;
using STF.Blog.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace STF.Blog.Service
{
    public class CommentService : ICommentService
    {
        private ICommentManager _icm;
        public CommentService(ICommentManager icm)
        {
            _icm = icm;
        }
        public async Task<int> AddCommentAsync(int bid, string content, int likesum, int uid)
        {
            return await _icm.AddAsync(new Model.Comment 
            {
                Bid = bid,
                Content = content,
                LikeSum = likesum,
                Uid = uid
            });
        }

        public async Task<int> DeleteCommentAsync(int id)
        {
            var data = await _icm.GetById(id);
            if (data == null)
                return -1;
            if (data.DeleteId != 1)
                return -2;
            return await _icm.DeleteAsync(data);
        }

        public async Task<List<CommentDto>> GetAllCommentAsync(int deleteId = 1)
        {
            var data = await _icm.GetChooseAsync(cm => cm.DeleteId == deleteId);
            return data == null ? new List<CommentDto>() : data.Select(cm => new CommentDto 
            {
                Id = cm.Id,
                Bid = cm.Bid,
                Content = cm.Content,
                LikeSum = cm.LikeSum,
                Uid = cm.Uid,
                CreateTime = cm.CreateTime,
                UpdateTime = cm.UpdateTime
            }).ToList();
        }

        public async Task<List<CommentDto>> GetByBidListAsync(int bid)
        {
            var data = await _icm.GetChooseAsync(cm => cm.Bid == bid);
            return data == null ? new List<CommentDto>() : data.Select(cm => new CommentDto
            {
                Id = cm.Id,
                Bid = cm.Bid,
                Content= cm.Content,
                LikeSum= cm.LikeSum,
                Uid= cm.Uid,
                CreateTime= cm.CreateTime
            }).OrderBy(cm => cm.CreateTime).ToList();
        }

        public async Task<CommentDto> GetByIdAsync(int id)
        {
            var data = await _icm.GetById(id);
            return data == null ? new CommentDto() : new CommentDto 
            {
                Id = data.Id,
                Bid = data.Bid,
                Content = data.Content,
                LikeSum = data.LikeSum,
                Uid = data.Uid,
                CreateTime = data.CreateTime,
                UpdateTime = data.UpdateTime
            };
        }

        public async Task<List<CommentDto>> GetByNameAsync(string content, int deleteId = 1)
        {
            var data = await _icm.GetChooseAsync(cm => cm.Content.Contains(content) && cm.DeleteId == deleteId);
            return data == null ? new List<CommentDto>() : data.Select(cm => new CommentDto 
            {
                Id = cm.Id,
                Bid = cm.Bid,
                Content = cm.Content,
                LikeSum = cm.LikeSum,
                Uid = cm.Uid,
                CreateTime = cm.CreateTime,
                UpdateTime = cm.UpdateTime
            }).ToList();
        }

        public async Task<int> PutTrashAsync(int id)
        {
            var data = await _icm.GetById(id);
            if (data == null || data.DeleteId == 0)
                return -1;
            data.DeleteId = 0;
            return await _icm.UpdateAsync(data);
        }

        public async Task<int> RestoreAsync(int id)
        {
            var data = await _icm.GetById(id);
            if (data == null || data.DeleteId == 1)
                return -1;
            data.DeleteId = 1;
            return await _icm.UpdateAsync(data);
        }

        public async Task<int> UpdateCommentAsync(int id, int bid, string content, int likesum, int uid)
        {
            var data = await _icm.GetById(id);
            if (data == null)
                return -1;
            data.Bid = bid;
            data.Content = content;
            data.LikeSum = likesum;
            data.Uid = uid;
            data.UpdateTime = DateTime.Now;
            return await _icm.UpdateAsync(data);
        }
    }
}
